/**
 * Created on May 18, 2007
 */
package jvs.vfs.sound.sampled.spi;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.AudioFileReader;

/**
 * @author qiangli
 * 
 */
public class BasicAudioFileReader extends AudioFileReader {
	private static AudioFileReader providers[] = null;

	public static void init(List<AudioFileReader> readers) {
		providers = (AudioFileReader[]) readers.toArray(new AudioFileReader[0]);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.sound.sampled.spi.AudioFileReader#getAudioFileFormat(java.io.File)
	 */
	public AudioFileFormat getAudioFileFormat(File file)
			throws UnsupportedAudioFileException, IOException {
		AudioFileFormat format = null;

		for (int i = 0; i < providers.length; i++) {
			try {
				format = providers[i].getAudioFileFormat(file);
				break;
			} catch (UnsupportedAudioFileException e) {
				continue;
			}
		}

		if (format == null) {
			throw new UnsupportedAudioFileException(
					"file is not a supported file type");
		} else {
			return format;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.sound.sampled.spi.AudioFileReader#getAudioFileFormat(java.io.InputStream)
	 */
	public AudioFileFormat getAudioFileFormat(InputStream stream)
			throws UnsupportedAudioFileException, IOException {
		AudioFileFormat format = null;

		for (int i = 0; i < providers.length; i++) {
			try {
				format = providers[i].getAudioFileFormat(stream);
				break;
			} catch (UnsupportedAudioFileException e) {
				continue;
			}
		}

		if (format == null) {
			throw new UnsupportedAudioFileException(
					"file is not a supported file type");
		} else {
			return format;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.sound.sampled.spi.AudioFileReader#getAudioFileFormat(java.net.URL)
	 */
	public AudioFileFormat getAudioFileFormat(URL url)
			throws UnsupportedAudioFileException, IOException {
		AudioFileFormat format = null;

		for (int i = 0; i < providers.length; i++) {
			try {
				format = providers[i].getAudioFileFormat(url);
				break;
			} catch (UnsupportedAudioFileException e) {
				continue;
			}
		}

		if (format == null) {
			throw new UnsupportedAudioFileException(
					"file is not a supported file type");
		} else {
			return format;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.sound.sampled.spi.AudioFileReader#getAudioInputStream(java.io.File)
	 */
	public AudioInputStream getAudioInputStream(File file)
			throws UnsupportedAudioFileException, IOException {
		AudioInputStream audioStream = null;

		for (int i = 0; i < providers.length; i++) {
			try {
				audioStream = providers[i].getAudioInputStream(file);
				break;
			} catch (UnsupportedAudioFileException e) {
				continue;
			}
		}

		if (audioStream == null) {
			throw new UnsupportedAudioFileException(
					"could not get audio input stream from input file");
		} else {
			return audioStream;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.sound.sampled.spi.AudioFileReader#getAudioInputStream(java.io.InputStream)
	 */
	public AudioInputStream getAudioInputStream(InputStream stream)
			throws UnsupportedAudioFileException, IOException {
		AudioInputStream audioStream = null;

		for (int i = 0; i < providers.length; i++) {
			try {
				audioStream = providers[i].getAudioInputStream(stream);
				break;
			} catch (UnsupportedAudioFileException e) {
				continue;
			}
		}

		if (audioStream == null) {
			throw new UnsupportedAudioFileException(
					"could not get audio input stream from input stream");
		} else {
			return audioStream;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.sound.sampled.spi.AudioFileReader#getAudioInputStream(java.net.URL)
	 */
	public AudioInputStream getAudioInputStream(URL url)
			throws UnsupportedAudioFileException, IOException {

		AudioInputStream audioStream = null;

		for (int i = 0; i < providers.length; i++) {
			try {
				audioStream = providers[i].getAudioInputStream(url);
				break;
			} catch (UnsupportedAudioFileException e) {
				continue;
			}
		}

		if (audioStream == null) {
			throw new UnsupportedAudioFileException(
					"could not get audio input stream from input URL");
		} else {
			return audioStream;
		}
	}
}
